package JUnitTests;

import static org.junit.Assert.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Calendar;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import DataStorageClasses.TimeSheet;
import Database.DatabaseConnector;

public class TimeSheetConnectorTests 
{
	private static String businessName = "testerBusiness";
	private static String businessPassword = "bus password";
	private static String clientFirst = "tester first";
	private static String clientLast = "tester last";
	private static long clientID;
	private static String employeeName = "emp first";
	private static long employeeID;
	private static long jobID;
	private static DatabaseConnector dbConnector;
	
	@BeforeClass public static void intitialize()
	{
		dbConnector = new DatabaseConnector();
		dbConnector.addBusiness(businessName, businessPassword, null, null, null, null, null, null, null, null, null);
		
		clientID = dbConnector.addClient(clientFirst, clientLast, null, null, null, null, null, null, null, null, businessName).getClientId();
		employeeID = dbConnector.addEmployee(businessName, employeeName, null, null, null, 15, -1, null, null, null, null, null, null).getEmployeeID();
		jobID = dbConnector.addNewJob(clientID, -1, -1, -1, null, null, null, null, null, null, null, businessName).getJobID();
	}
	
	@AfterClass public static void destroy()
	{
		dbConnector.removeJob(jobID);
		dbConnector.removeClient(clientID);
		dbConnector.removeBusiness(businessName, businessPassword);
		dbConnector.disconnect();
	}

	@Test public void testAddTimeSheet()
	{
		Date date = new Date(21);
		TimeSheet tSheet = dbConnector.addTimeSheet(jobID, employeeID, date, 8, false);
		assertNotNull(tSheet);
		
		assertTrue(jobID == tSheet.getJobID());
		assertTrue(employeeID == tSheet.getEmployeeID());
		assertArrayEquals(date.toString().toCharArray(), tSheet.getDate().toString().toCharArray());
		
		tSheet = dbConnector.addTimeSheet(jobID, employeeID, date, 8, false);
		assertNull(tSheet);
		
		boolean removed = dbConnector.removeTimeSheet(employeeID, jobID, date);
		assertTrue(removed);
	}
	
	@Test public void testGetTimeSheet()
	{
		Date date = new Date(0);
		dbConnector.addTimeSheet(jobID, employeeID, date, 4, true);
		TimeSheet tSheet = dbConnector.getTimeSheet(jobID, employeeID, date);
		assertNotNull(tSheet);
		
		dbConnector.removeTimeSheet(employeeID, jobID, date);
	}
	
	@Test public void testEditTimeSheet()
	{
		Date date = new Date(1231322312);
		double hours = 2;
		boolean payed = true;
		
		dbConnector.addTimeSheet(jobID, employeeID, date, 8, false);
		boolean edited = dbConnector.editTimeSheet(jobID, employeeID, date, hours, payed);
		assertTrue(edited);
		
		TimeSheet tSheet = dbConnector.getTimeSheet(jobID, employeeID, date);
		
		assertNotNull(tSheet);
		assertTrue(hours == tSheet.getHours());
		assertTrue(payed == tSheet.wasPayed());
		
		dbConnector.removeTimeSheet(employeeID, jobID, date);
	}
	
	@Test public void testGetEmployeeTimeSheets()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.set(2009, 3, 5);
		Date date1 = new Date(calendar.getTimeInMillis());
		calendar.set(2009, 3, 6);
		Date date2 = new Date(calendar.getTimeInMillis());
		calendar.set(2009, 3, 7);
		Date date3 = new Date(calendar.getTimeInMillis());
		
		dbConnector.addTimeSheet(jobID, employeeID, date1, 9, false);
		dbConnector.addTimeSheet(jobID, employeeID, date2, 1, false);
		dbConnector.addTimeSheet(jobID, employeeID, date3, 1, false);
		
		System.out.println(dbConnector.getTotalWages(businessName, null, null));
		ArrayList<TimeSheet> tSheets = dbConnector.getEmployeesTimeSheets(employeeID, null, null, false);
		assertTrue(tSheets.size() == 3);
		
		dbConnector.removeTimeSheet(employeeID, jobID, date1);
		dbConnector.removeTimeSheet(employeeID, jobID, date2);
		dbConnector.removeTimeSheet(employeeID, jobID, date3);
	}
	
	@Test public void testRemoveTimeSheet()
	{
		Date date = new Date(1231322312);
		
		dbConnector.addTimeSheet(jobID, employeeID, date, 8, false);
		boolean removed = dbConnector.removeTimeSheet(employeeID, jobID, new Date(0));
		assertFalse(removed);
		
		removed = dbConnector.removeTimeSheet(-1, jobID, date);
		assertFalse(removed);
		
		removed = dbConnector.removeTimeSheet(employeeID, -1, date);
		assertFalse(removed);
		
		removed = dbConnector.removeTimeSheet(employeeID, jobID, date);
		assertTrue(removed);
	}
	
	@Test public void testMarkTimeSheetsPayed()
	{
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(2010, 3, 3);
		Date date1 = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 4, 4);
		Date date2 = new Date(calendar.getTimeInMillis());
		
		calendar.set(2010, 2, 2);
		Date start = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 5, 5);
		Date finished = new Date(calendar.getTimeInMillis());
		
		dbConnector.addTimeSheet(jobID, employeeID, date1, 2, true);
		dbConnector.addTimeSheet(jobID, employeeID, date2, 5, false);
		
		boolean updated = dbConnector.markTimeSheetsPayed(employeeID, start, finished);
		assertTrue(updated);
		
		dbConnector.removeTimeSheet(employeeID, jobID, date1);
		dbConnector.removeTimeSheet(employeeID, jobID, date2);
	}
	
	@Test public void testGetTimeSheets()
	{
		Calendar calendar = Calendar.getInstance();
		
		calendar.set(2010, 3, 3);
		Date date1 = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 4, 4);
		Date date2 = new Date(calendar.getTimeInMillis());
		
		calendar.set(2010, 2, 2);
		//Date start = new Date(calendar.getTimeInMillis());
		calendar.set(2010, 5, 5);
		//Date finished = new Date(calendar.getTimeInMillis());
		
		long job2 = dbConnector.addNewJob(clientID, 12, 32, 34, null, null, null, null, null, null, null, businessName).getJobID();
		dbConnector.addTimeSheet(jobID, employeeID, date1, 2, true);
		dbConnector.addTimeSheet(jobID, employeeID, date2, 5, false);
		dbConnector.addTimeSheet(job2, employeeID, date2, 5, false);
		
		ArrayList<TimeSheet> sheets = dbConnector.getTimeSheets(job2, -1, null, null, businessName);
		assertTrue(sheets.size() == 1);
		
		sheets = dbConnector.getTimeSheets(-1, -1, null, null, businessName);
		System.out.println(sheets.size());
		//assertTrue(sheets.size() == 3);
		
		dbConnector.removeTimeSheet(employeeID, jobID, date1);
		dbConnector.removeTimeSheet(employeeID, jobID, date2);
		dbConnector.removeTimeSheet(employeeID, job2, date2);
		dbConnector.removeJob(job2);
	}
}
